//
//  HJGroupSettingController.m
//  KaiChat
//
//  Created by lx on 2023/11/6.
//

#import "HJGroupSettingController.h"
#import "HJGroupIntroductionCell.h"
#import "HJGroupMemberContentCell.h"
#import "HJGroupNoticeCell.h"
#import "HJSwitchCell.h"
#import "HJTitleCell.h"
#import "HJCenterTitleCell.h"
#import "HJIM.h"
#import "HJGroupMemberModel.h"
#import "HJEditGroupInfoController.h"

@interface HJGroupSettingController ()

@property (nonatomic, strong) NSArray <NSArray <HJCellModel *> * >*datas;
@property (nonatomic, strong) NSMutableDictionary *modelDict;

@end

@implementation HJGroupSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.title = @"Group Information";
    
    [self.tableView registerClass:HJGroupIntroductionCell.class forCellReuseIdentifier:@"HJGroupIntroductionCell"];
    [self.tableView registerClass:HJGroupMemberContentCell.class forCellReuseIdentifier:@"HJGroupMemberContentCell"];
    [self.tableView registerClass:HJGroupNoticeCell.class forCellReuseIdentifier:@"HJGroupNoticeCell"];
    [self.tableView registerClass:HJSwitchCell.class forCellReuseIdentifier:@"HJSwitchCell"];
    [self.tableView registerClass:HJTitleCell.class forCellReuseIdentifier:@"HJTitleCell"];
    [self.tableView registerClass:HJCenterTitleCell.class forCellReuseIdentifier:@"HJCenterTitleCell"];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, TabbarSafeBottomMargin, 0);
    self.tableView.backgroundColor = UIColor.tj_F6F6F6Color;
    self.view.backgroundColor = UIColor.tj_F6F6F6Color;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.bottom.offset(0);
    }];
    
    [self loadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.datas[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJCellModel *model = self.datas[indexPath.section][indexPath.row];
    if ([model.identifier isEqualToString:@"introduction"]) {
        HJGroupIntroductionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJGroupIntroductionCell" forIndexPath:indexPath];
        TJWeakify(self)
        [cell setDidClickEditButtonBlock:^{
            
            HJEditGroupInfoController *editGroupInfoController = [[HJEditGroupInfoController alloc] init];
            editGroupInfoController.groupModel = weakself.groupModel;
            [weakself.navigationController pushViewController:editGroupInfoController];
            
        }];
        cell.model = model;
        return cell;
    }
    
    if ([model.identifier isEqualToString:@"administrators"] || [model.identifier isEqualToString:@"members"]) {
        HJGroupMemberContentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJGroupMemberContentCell" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    
    if ([model.identifier isEqualToString:@"notice"]) {
        HJGroupNoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJGroupNoticeCell" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    
    if ([model.identifier isEqualToString:@"founder"] || [model.identifier isEqualToString:@"disband"]) {
        HJCenterTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJCenterTitleCell" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    
    if ([model.identifier isEqualToString:@"mute"] || [model.identifier isEqualToString:@"top"]) {
        HJSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJSwitchCell" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    
    HJTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJTitleCell" forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    return view;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColor.clearColor;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJCellModel *model = self.datas[indexPath.section][indexPath.row];
    if (model.height > 0) return model.height;
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}

- (void)loadData {
    
    self.groupModel.intro = @"Sign in, clock in, and send U activity is here! (Only new friends who join the group by sharing red envelopes)";
    
    
    void (^completion)(NSArray<HJGroupMemberModel *> * _Nullable datas) = ^(NSArray<HJGroupMemberModel *> * _Nullable datas){
        
        NSMutableArray *administratorList = [NSMutableArray array];
        NSMutableArray *userList = [NSMutableArray array];
                
        [datas enumerateObjectsUsingBlock:^(HJGroupMemberModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx > 8) {
                *stop = YES;
            }
            
            if (obj.role == 1 || obj.role == 2) {
                [administratorList addObject:obj];
            }
            [userList addObject:obj];
        }];
        
        HJGroupMemberModel *addModel = [[HJGroupMemberModel alloc] init];
        addModel.userId = @"add";
        addModel.avatar = @"edit_add";
        
        HJGroupMemberModel *removeModel = [[HJGroupMemberModel alloc] init];
        removeModel.userId = @"remove";
        removeModel.avatar = @"edit_minus";
        
        [userList addObject:addModel];
        [userList addObject:removeModel];
        
        [administratorList addObject:addModel];
        [administratorList addObject:removeModel];
        
        HJCellModel *administratorsModel = [self.modelDict objectForKey:@"administrators"];
        administratorsModel.anyObject = administratorList;
        administratorsModel.height = (1 + (administratorList.count - 1) / 5) * 85 + 35;

        HJCellModel *membersModel = [self.modelDict objectForKey:@"members"];
        membersModel.anyObject = userList;
        membersModel.height = (userList.count > 5 ? 2 : 1) * 85 + 35;

        self.groupModel.userList = userList;
        self.groupModel.administratorList = administratorList;

        dispatch_async_main_queue(^{
            [self.tableView reloadData];
        });
    };
    
    [HJIM.shareIM.groupManager queryGroupMemberListWithGroupId:self.groupModel.groupId completion:completion];
    [HJIM.shareIM.groupManager loadGroupMemberListWithGroupId:self.groupModel.groupId completion:completion];
}

- (NSArray *)datas {
    if (!_datas) {
        _datas = @[
            @[
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"introduction";//如果是群主就可以编辑
                    model.height = 80;
                    model.anyObject = self.groupModel;
                    [self.modelDict setObject:model forKey:model.identifier];
                }]
            ],
            @[
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"administrators";
                    model.text = @"Administrators";
                    model.anyObject = self.groupModel.administratorList;
                    NSInteger row = 1 + (self.groupModel.administratorList.count - 1) / 5;
                    model.height = row * 85 + 35 + 10;
                    [self.modelDict setObject:model forKey:model.identifier];
                }],
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"members";
                    model.text = [NSString stringWithFormat:@"Group Members (%@)", self.groupModel.memberNums];
                    model.anyObject = self.groupModel.userList;
                    NSInteger row = self.groupModel.userList.count > 5 ? 2 : 1;
                    model.height = row * 85 + 35 + 10;
                    [self.modelDict setObject:model forKey:model.identifier];
                }]
            ],
            @[
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"notice";
                    model.height = 70;
                    model.text = @"Group notice";
                    [self.modelDict setObject:model forKey:model.identifier];
                }],
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"groupManager";
                    model.text = @"Manage Group";
                }],
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"mute";
                    model.text = @"Mute";
                    [self.modelDict setObject:model forKey:model.identifier];
                }],
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"top";
                    model.text = @"Top";
                    [self.modelDict setObject:model forKey:model.identifier];
                }],
            ],
            @[
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"leave";
                    model.text = @"Leave Group";
                }]
            ],
            @[
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"founder";
                    model.text = @"Select new founder";
                }]
            ],
            @[
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"disband";
                    model.text = @"Disband group";
                    model.textColor = UIColor.tj_FF5100Color;
                }]
            ],
            
        ];
    }
    return _datas;
}

- (NSMutableDictionary *)modelDict {
    if (!_modelDict) {
        _modelDict = [NSMutableDictionary dictionary];
    }
    return _modelDict;
}

@end
